package com.sgb.kjwl.viewmodel.self.packers;


import android.app.Application;
import android.content.Context;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.packers.TeamAlbumEntity;
import com.sgb.kjwl.model.entity.self.SelfTeamAlbumEntity;
import com.sgb.kjwl.model.entity.self.TeamUploadAlbumEntity;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.activity.self.packers.SelfTeamAlbumActivity;
import com.sgb.net.entity.ListResponse;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.common.base.BaseViewModel;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import org.greenrobot.eventbus.EventBus;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class SelfTeamAlbumModel extends BaseViewModel {

    public MutableLiveData<List<String>> uploadUrls=new MutableLiveData<>();

    public MutableLiveData<ListResponse<List<TeamAlbumEntity>>> ldAlbum=new MutableLiveData<>();

    public MutableLiveData<Boolean> ldDelResult=new MutableLiveData<>();


    public int pageNum = 1;
    private int limit = 10;

    public SelfTeamAlbumModel(@NonNull @NotNull Application application) {
        super(application);
    }


    public void requestTeamAlbumData(boolean isLoadMore) {
        pageNum = isLoadMore ? pageNum + 1 : 1;
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().getTeamAlbumList(limit,pageNum).enqueue(new ICallBack<BaseEntity<ListResponse<List<TeamAlbumEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<TeamAlbumEntity>>> data) {
                if(data.isSuccess()){
                    ldAlbum.postValue(data.getData());
                }
            }
        });
    }

    /**
     * 保存到服务器
     * @param entity
     */
    public void savePhotoData(Context context, List<TeamAlbumEntity> entity, View view) {
        view.setEnabled(false);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().saveTeamAlbum(entity).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                if(data.isSuccess()){
                    MToast.showToast(context,"删除成功");
                    ldDelResult.postValue(true);

                }else{
                    ldDelResult.postValue(false);
                }
                view.setEnabled(true);
            }

            @Override
            public void onFailureWithEntity(BaseEntity entity) {
                super.onFailureWithEntity(entity);
                ldDelResult.postValue(false);
                view.setEnabled(true);
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                super.onFailure(call, t);
                ldDelResult.postValue(false);
                view.setEnabled(true);
            }
        });


    }
    /**
     * 保存到服务器
     * @param entity
     */
    public void savePhotoData(Context context, List<TeamAlbumEntity> entity) {

        NewAPIManager.getInstance().getTeamNewInterfaceAPI().saveTeamAlbum(entity).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                if(data.isSuccess()){
                    MToast.showToast(context,"保存成功");

                }

            }
        });


    }
    public void uploadMutiFileModel(List<File> files) {
        Map<String, RequestBody> photos = new HashMap<>();
        int i = 0;
        for (File file : files) {
            RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
            photos.put("file" +i+ "\"; filename=\"" + i + "image.jpg\"", requestBody);
            i++;
        }
        APIManager.getInstance().getUploadAPI().uploadMutiFileDrive(photos).enqueue(new ICallBack<BaseEntity<List<String>>>() {
            @Override
            public void onSuccess(BaseEntity<List<String>> data) {
                if(data.isSuccess()){
                    uploadUrls.postValue(data.getData());

                }

            }
        });
    }
}
